﻿using SF.Common;
using SF.Ucenter.shallWeChat;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.mycenterusers
{
    public partial class qrcode : System.Web.UI.Page
    {
        Model.sf_user_info userInfo;
        string appWhere;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*验证用户是否登录*/
                if (Session[DTKeys.SESSION_ADMIN_INFO] == null)
                {
                    Response.Redirect("index.aspx");
                }
                userInfo = Session[DTKeys.SESSION_ADMIN_INFO] as Model.sf_user_info;
                appWhere = Utils.getWhereByInfo(userInfo.busnieseNum, userInfo.appNum, userInfo.templatesNum);
                string path = "/allqrcode/imgCode/" + userInfo.inviteKey + ".jpg";
                if (!File.Exists(Utils.GetMapPath(path)))
                {
                    string codestr = "/allqrcode/code/" + userInfo.inviteKey + ".jpg";
                    if (!File.Exists(Utils.GetMapPath(codestr)))
                    {
                        IDictionary dict = System.Configuration.ConfigurationManager.GetSection("weixinPay") as IDictionary;
                        string inf_uri = dict["inf_uri"].ToString();
                        string temp_uri = dict["temp_uri"].ToString();
                        string qrcode_uri = dict["qrcode_uri"].ToString();
                        string appNum = userInfo.appNum;
                        string appSecret = new BLL.sf_appidmanage().GetModel(userInfo.appNum).appsecret;
                        api.wxpay.WebClient obj = new api.wxpay.WebClient();
                        //获取微信接口的access_token
                        string str3 = obj.GetHtml(inf_uri + "grant_type=client_credential&appid=" + appNum + "&secret=" + appSecret);
                        wxaccesstoken ak = api.wxpay.JsonHelper.JsonDeserialize<wxaccesstoken>(str3);

                        //获取 二维码 对象  用于换取 二维码资源 链接
                        string str4 = obj.Post(temp_uri + "access_token=" + ak.access_token, "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + userInfo.inviteKey + "\"}}}");
                        shallWeChat.qrcode qr = api.wxpay.JsonHelper.JsonDeserialize<shallWeChat.qrcode>(str4);

                        /*通过ticket换二维码*/
                        string imgs = Server.UrlEncode(qr.ticket);
                        obj.DownloadFile(qrcode_uri + "ticket=" + imgs, Utils.GetMapPath("/allqrcode/code/" + userInfo.inviteKey + ".jpg"));
                    }
                    allqrcode.QrCode code = new allqrcode.QrCode();
                    this.codeImg.ImageUrl = code.getQrCodeImg(userInfo.inviteKey,appWhere,0);
                }
                else
                {
                    this.codeImg.ImageUrl = path;
                }
            }
        }
    }
}